﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.IO;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace VehicleRecordSystem.view
{
    /// <summary>
    /// Interaction logic for VehiclePage.xaml
    /// </summary>
    public partial class VehiclePage : Page
    {
        private static VehiclePage instanse;
        private string errorBackgroundColor = "#EBA4B0";

        public static VehiclePage Instanse
        {
            get
            {
                if (instanse == null)
                {
                    instanse = new VehiclePage();
                }

                return instanse;
            }
        }
        public VehiclePage()
        {
            InitializeComponent();
        }

        private void btnVehicalPageSave_Click(object sender, RoutedEventArgs e)
        {
            if (validate())
            {
                string registration_number = regNo_textBox.Text;
                string category = category_textBox.Text;
                string type = type_textBox.Text;
                string make = make_textBox.Text;
                string model = model_textBox.Text;
                string fuel_type = fuel_comboBox.Text;
                string engine_number = engineNo_textBox.Text;
                string chassis_number = chassisNo_textBox.Text;
                string manufactured_year = manufacYr_textBox.Text;
                DateTime? imported_date = importedDate_Box.SelectedDate;
                DateTime? registered_date = regDate_Box.SelectedDate;
                BitmapImage registration_certificate_image = new BitmapImage(new Uri(image_textBox.Text));
                string description = desc_textBox.Text;
            }

          /*  VehicleDB db = new VehicleDB();

            vrs_vehicle vehicle = new vrs_vehicle();

            vehicle.REGISTRATION_NUMBER = registration_number;
            vehicle.CATEGORY = category;
            vehicle.TYPE = type;
            vehicle.MAKE = make;
            vehicle.MODEL = model;
            vehicle.FUEL_TYPE = fuel_type;
            vehicle.ENGINE_NUMBER = engine_number;
            vehicle.CHASSIS_NUMBER = chassis_number;
            vehicle.YEAR_MANUFACTURE = year;
            //vehicle.DATE_IMPORT = DateTime.ParseExact(imported_date.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
            //vehicle.DATE_REGISTER = DateTime.ParseExact(reg_date.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
            //vehicle.REGISTRATION_CERTIFICATE = image_textBox.Text;
            vehicle.DESCRIPTION = desc_textBox.Text;

            

            using (db)
            {

                db.Vehicle.Add(vehicle);


                db.SaveChanges();
            }
            */
        }



        private void btnCertificateBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.ShowDialog();
                openFileDialog.Title = "Set Insurance Card Image";
                openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
                openFileDialog.DefaultExt = ".jpeg";
                image_textBox.Text = openFileDialog.FileName;

                ImageSource imageSource = new BitmapImage(new Uri(image_textBox.Text));
                imageRegistrationCertificate.Source = imageSource;
            }
            catch(Exception)
            {
                MessageBox.Show("Image Uploading Error","Error", MessageBoxButton.OK,MessageBoxImage.Error);
            }

        }
        private void btnShowVehicalViewPage_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instanse.ContentFrame.Content = VehicleViewPage.Instanse;
        }

        private Boolean validate()
        {
            Boolean ok = true;
            var bc = new BrushConverter();

            if (string.IsNullOrEmpty(regNo_textBox.Text))
            {
                ok = false;
                regNo_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(category_textBox.Text))
            {
                ok = false;
                category_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(type_textBox.Text))
            {
                ok = false;
                type_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(make_textBox.Text))
            {
                ok = false;
                make_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(model_textBox.Text))
            {
                ok = false;
                model_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(fuel_comboBox.Text))
            {
                ok = false;
                fuel_comboBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(engineNo_textBox.Text))
            {
                ok = false;
                engineNo_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(chassisNo_textBox.Text))
            {
                ok = false;
                chassisNo_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(manufacYr_textBox.Text))
            {
                ok = false;
                manufacYr_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (!validUri(image_textBox.Text))
            {
                ok = false;
                image_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(importedDate_Box.SelectedDate.ToString()))
            {
                ok = false;
                importedDate_Box.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(regDate_Box.SelectedDate.ToString()))
            {
                ok = false;
                regDate_Box.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(desc_textBox.Text))
            {
                ok = false;
                desc_textBox.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }            

            return ok;
        }

        private Boolean validUri(String path)
        {
            Boolean ok = true;
            try
            {
                new Uri(path);
            }
            catch
            {
                ok = false;
            }
            return ok;
        }
        private void changeBackgroundColorToNormal(object sender, RoutedEventArgs e)
        {
            var bc = new BrushConverter();

            if (sender is TextBox )
            {
                TextBox txt = (TextBox)sender;
                txt.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is DatePicker)
            {
                DatePicker dp = (DatePicker)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is ComboBox)
            {
                ComboBox dp = (ComboBox)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
        }


            
    }

}